/*******************************************************************************
 * Copyright (c) 2010 Torkild U. Resheim
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Torkild U. Resheim - initial API and implementation
 *******************************************************************************/
package no.resheim.buildmonitor.ui.properties;

import no.resheim.buildmonitor.core.data.Job;
import no.resheim.buildmonitor.ui.BuildmonitorUI;

import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertyDescriptor;

public class JobPropertySource implements IPropertySource {

	public static final String ACTIVE = "active";
	public static final String BUILDABLE = "buildable";
	public static final String CHANGE_DESCRIPTION = "changeDescription";
	public static final String DESCRIPTION = "description";
	public static final String DISPLAY_NAME = "displayName";
	public static final String FIRST_BUILD_NUMBER = "firstBuildNumber";
	public static final String HEALTH_REPORT = "healthReport";
	public static final String IN_QUEUE = "inQueue";
	public static final String LAST_COMPLETED_BUILD_NUMBER = "lastCompletedBuildNumber";
	public static final String LAST_FAILED_BUILD_NUMBER = "lastFailedBuildNumber";
	public static final String LAST_STABLE_BUILD_NUMBER = "lastStableBuildNumber";
	public static final String LAST_STARTED = "lastStarted";
	public static final String LAST_SUCCESSFUL_BUILD_NUMBER = "lastSuccessfulBuildNumber";
	public static final String NAME = "name";
	public static final String URI = "uri";
	public static final String LAST_BUILD = "lastBuild";
	public static final String ETA = "estimatedCompletion";

	private final Job job;

	public JobPropertySource(Job job) {
		super();
		this.job = job;
	}

	@Override
	public Object getEditableValue() {
		return this;
	}

	@Override
	public IPropertyDescriptor[] getPropertyDescriptors() {
		return new IPropertyDescriptor[] {
				new PropertyDescriptor(NAME, "Name"),
				new PropertyDescriptor(DISPLAY_NAME, "Display name"),
				new PropertyDescriptor(LAST_BUILD, "Last build"),
				new PropertyDescriptor(ETA, "Estimated completion"),
				new PropertyDescriptor(FIRST_BUILD_NUMBER, "First build"),
				new PropertyDescriptor(LAST_STARTED, "Last started build"),
				new PropertyDescriptor(LAST_FAILED_BUILD_NUMBER,
						"Last failed build"),
				new PropertyDescriptor(LAST_SUCCESSFUL_BUILD_NUMBER,
						"Last sucessful build"),
				new PropertyDescriptor(IN_QUEUE, "Queued"),
				new PropertyDescriptor(CHANGE_DESCRIPTION, "Change description"),
				new PropertyDescriptor(DESCRIPTION, "Description"),
				new PropertyDescriptor(ACTIVE, "Active"),
				new PropertyDescriptor(HEALTH_REPORT, "Health report"),
				new PropertyDescriptor(BUILDABLE, "Buildable"),
				new PropertyDescriptor(URI, "URI"),
				new PropertyDescriptor(LAST_STABLE_BUILD_NUMBER,
						"Last stable build"),
				new PropertyDescriptor(LAST_COMPLETED_BUILD_NUMBER,
						"Last completed build") };
	}

	@Override
	public Object getPropertyValue(Object id) {
		return getPropertyValue(job, id);
	}

	public static Object getPropertyValue(Job job, Object id) {
		if (id.equals(BUILDABLE))
			return job.isBuildable();
		if (id.equals(URI))
			return job.getUri();
		if (id.equals(NAME))
			return job.getName();
		if (id.equals(LAST_BUILD))
			return BuildmonitorUI.formatBuild(job.getLastStartedBuild());
		if (id.equals(ETA)) {
			if (job.getLastStartedBuild() == null || !job.isActive()) {
				return "";
			} else {
				return BuildmonitorUI.formatDuration(job
						.getEstimatedCompletion());
			}
		}
		if (id.equals(LAST_COMPLETED_BUILD_NUMBER))
			return job.getLastCompletedBuildNumber();
		if (id.equals(LAST_STABLE_BUILD_NUMBER))
			return job.getLastStableBuildNumber();
		if (id.equals(DISPLAY_NAME))
			return job.getDisplayName();
		if (id.equals(FIRST_BUILD_NUMBER))
			return job.getFirstBuildNumber();
		if (id.equals(LAST_STARTED))
			return job.getLastStartedBuildNumber();
		if (id.equals(LAST_FAILED_BUILD_NUMBER))
			return job.getLastFailedBuildNumber();
		if (id.equals(LAST_SUCCESSFUL_BUILD_NUMBER))
			return job.getLastSuccessfulBuildNumber();
		if (id.equals(IN_QUEUE))
			return job.isInQueue();
		if (id.equals(CHANGE_DESCRIPTION))
			return job.getChangeDescription();
		if (id.equals(DESCRIPTION))
			return job.getDescription();
		if (id.equals(ACTIVE))
			return job.isActive();
		if (id.equals(HEALTH_REPORT))
			return job.getHealthReport();

		return null;

	}

	@Override
	public boolean isPropertySet(Object id) {
		return false;
	}

	@Override
	public void resetPropertyValue(Object id) {
	}

	@Override
	public void setPropertyValue(Object id, Object value) {
	}

}
